package com.yjyw.tiexie.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjyw.tiexie.MainActivity;
import com.yjyw.tiexie.R;
import com.yjyw.tiexie.bean.LoginResultBean;
import com.yjyw.tiexie.bean.event.LoginEvent;
import com.yjyw.tiexie.common.BaseActivity;
import com.yjyw.tiexie.common.net.HandlerCode;
import com.yjyw.tiexie.common.net.NetAPI;
import com.yjyw.tiexie.common.net.NetCallBack;
import com.yjyw.tiexie.config.BaseUIConfig;
import com.yjyw.tiexie.util.GsonUtil;
import com.yjyw.tiexie.util.SharedPreferencesUtils;
import com.yjyw.tiexie.util.StringUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 2019/7/12.
 *
 * @author ${AUTHOR}
 * TODO:
 */
public class LoginActivity extends BaseActivity implements NetCallBack {


    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.ll_top)
    FrameLayout llTop;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.ll_select)
    LinearLayout llSelect;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.iv_selected)
    ImageView ivSelected;


    private boolean isSelected;


    private static final String TAG = LoginActivity.class.getSimpleName();

    private int mUIType;
    private BaseUIConfig mUIConfig;

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }


    @Override
    protected void init() {
        EventBus.getDefault().register(this);

        isSelected = (boolean) SharedPreferencesUtils.getParam(this, "isSelected", false);
        if (isSelected) {
            ivSelected.setBackgroundResource(R.drawable.icon_selected_red_friend);
            editName.setText((String) SharedPreferencesUtils.getParam(this, "account", ""));
            editPassword.setText((String) SharedPreferencesUtils.getParam(this, "password", ""));
        } else {
            ivSelected.setBackgroundResource(R.drawable.icon_selected_gray_friend);
        }
    }


    @Subscribe
    public void ChangeTab(LoginEvent mainChangeEvent) {
        finish();
    }


    @Override
    public void tokenTimeOut(int requestCode) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void onResponse(boolean flag, int requestCode, String result) throws Exception {

        switch (requestCode) {
            case HandlerCode.LOGIN:
                hideProgress();
                LoginResultBean loginResultBean = (LoginResultBean) GsonUtil.getObject(result, LoginResultBean.class);
                if (loginResultBean.getCode() == 100) {
                    if (loginResultBean.getData() != null) {
                        String userId = loginResultBean.getData();
                        SharedPreferencesUtils.setParam(this, "userId", userId);
                        if (isSelected) {
                            SharedPreferencesUtils.setParam(this, "isSelected", true);
                            SharedPreferencesUtils.setParam(this, "account", editName.getText().toString());
                            SharedPreferencesUtils.setParam(this, "password", editPassword.getText().toString());
                        } else {
                            SharedPreferencesUtils.setParam(this, "isSelected", false);
                            SharedPreferencesUtils.setParam(this, "account", "");
                            SharedPreferencesUtils.setParam(this, "password", "");
                        }
                        startActivity(new Intent(this, MainActivity.class));
                    }
                } else {
                    toast(loginResultBean.getMessage());
                }
                break;
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.tv_content, R.id.ll_top, R.id.ll_select, R.id.tv_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_content:
                break;
            case R.id.ll_top:
                break;
            case R.id.ll_select:
                if (isSelected) {
                    ivSelected.setBackgroundResource(R.drawable.icon_selected_gray_friend);
                } else {
                    ivSelected.setBackgroundResource(R.drawable.icon_selected_red_friend);
                }
                isSelected = !isSelected;
                break;
            case R.id.tv_login:
                if (StringUtil.isEmpty(editName.getText().toString())) {
                    toast("请输入账号");
                    return;
                }
                if (StringUtil.isEmpty(editPassword.getText().toString())) {
                    toast("请输入密码");
                    return;
                }
                showProgress("");
                HashMap map = new HashMap();
                map.put("account", editName.getText().toString());
                map.put("password", editPassword.getText().toString());
                postJson(map, NetAPI.LOGIN, HandlerCode.LOGIN, this);
                break;
        }
    }
}
